<html>
<head>
<title>ShowProductsActivity.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(0,0,255); }
.s3 { color: rgb(128,128,128); font-style: italic; }
.s4 { color: rgb(0,128,0); font-weight: bold; }
.s5 { color: rgb(128,128,128); font-weight: bold; font-style: italic; }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
ShowProductsActivity.java</FONT>
</center></TD></TR></TABLE>
<pre>

<span class="s0">package </span><span class="s1">com.example.lista_zakupow.activities.products; 
 
</span><span class="s0">import </span><span class="s1">android.app.Activity; 
</span><span class="s0">import </span><span class="s1">android.app.AlertDialog; 
</span><span class="s0">import </span><span class="s1">android.app.Dialog; 
</span><span class="s0">import </span><span class="s1">android.content.DialogInterface; 
</span><span class="s0">import </span><span class="s1">android.content.Intent; 
</span><span class="s0">import </span><span class="s1">android.os.Bundle; 
</span><span class="s0">import </span><span class="s1">android.view.View; 
</span><span class="s0">import </span><span class="s1">android.widget.*; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.R; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.activities.MainActivity; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.adapters.SpecialAdapter; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.database.dao.ProduktyDao; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.ListaZakupow; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.Produkt; 
 
</span><span class="s0">import </span><span class="s1">java.util.List; 
 
</span><span class="s0">public class </span><span class="s1">ShowProductsActivity </span><span class="s0">extends </span><span class="s1">Activity { 
    </span><span class="s0">private static final int </span><span class="s1">CONFIRM_REMOVE_LIST = </span><span class="s2">1</span><span class="s1">; 
 
    SpecialAdapter&lt;Produkt&gt; adapter; 
    ProduktyDao produktyDao; 
 
    </span><span class="s0">private </span><span class="s1">ListView list; 
 
    </span><span class="s0">private </span><span class="s1">TextView howMayTasks; 
 
    </span><span class="s0">private </span><span class="s1">Button addBtn; 
    </span><span class="s0">private </span><span class="s1">Button editBtn; 
    </span><span class="s0">private </span><span class="s1">Button deleteBtn; 
    </span><span class="s0">private </span><span class="s1">Button showProductsBtn; 
 
    ListaZakupow lista; 
 
    @Override 
    </span><span class="s0">protected void </span><span class="s1">onCreate(Bundle savedInstanceState) { 
        </span><span class="s0">super</span><span class="s1">.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        initialize(); 
        setOnClickListeners(); 
    } 
 
    </span><span class="s3">/** 
     * Inicjalizacja danych 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">initialize() { 
        list = (ListView) findViewById(R.id.list); 
        lista = (ListaZakupow) getIntent().getExtras().get(</span><span class="s4">&quot;lista&quot;</span><span class="s1">); 
 
        produktyDao = </span><span class="s0">new </span><span class="s1">ProduktyDao(</span><span class="s0">this</span><span class="s1">, lista); 
        List&lt;Produkt&gt; listaProduktow = produktyDao.getAllProductsByShopList(); 
 
        adapter = </span><span class="s0">new </span><span class="s1">SpecialAdapter&lt;Produkt&gt;(</span><span class="s0">this</span><span class="s1">, listaProduktow); 
        list.setAdapter(adapter); 
 
        howMayTasks = (TextView) findViewById(R.id.textViewHowMayTasks); 
 
        addBtn = (Button) findViewById(R.id.buttonAdd); 
        editBtn = (Button) findViewById(R.id.buttonEdit); 
        deleteBtn = (Button) findViewById(R.id.buttonDelete); 
        showProductsBtn = (Button) findViewById(R.id.showProdBtn); 
        showProductsBtn.setVisibility(View.INVISIBLE); 
 
        refreshListView(); 
 
        disableBtns(); 
    } 
 
    </span><span class="s3">/** 
     * Ustawienie słuchaczy 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">setOnClickListeners() { 
        addBtn.setOnClickListener(</span><span class="s0">new </span><span class="s1">ButtonAddListener()); 
        editBtn.setOnClickListener(</span><span class="s0">new </span><span class="s1">ButtonEditListener()); 
        deleteBtn.setOnClickListener(</span><span class="s0">new </span><span class="s1">ButtonDeleteListener()); 
        list.setOnItemClickListener(</span><span class="s0">new </span><span class="s1">SluchaczKliknieciaNaListe()); 
        list.setOnItemLongClickListener(</span><span class="s0">new </span><span class="s1">SluchaczListyDlugieKlikniecie()); 
    } 
 
    </span><span class="s3">/** 
     * Włączenie przycisków 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">enableBtns() { 
        addBtn.setEnabled(</span><span class="s0">true</span><span class="s1">); 
        editBtn.setEnabled(</span><span class="s0">true</span><span class="s1">); 
        deleteBtn.setEnabled(</span><span class="s0">true</span><span class="s1">); 
    } 
 
    </span><span class="s3">/** 
     * Wyłączenie przycisków 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">disableBtns() { 
        editBtn.setEnabled(</span><span class="s0">false</span><span class="s1">); 
        deleteBtn.setEnabled(</span><span class="s0">false</span><span class="s1">); 
    } 
 
    </span><span class="s3">/** 
     * Odświeżenie informacji o ilości produktów na liście 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">refreshListView() { 
        </span><span class="s0">if </span><span class="s1">(list.getCount() == </span><span class="s2">0</span><span class="s1">) { 
            howMayTasks.setText(</span><span class="s4">&quot;Brak produktów na liście zakupów: &quot; </span><span class="s1">+ lista.getNameToView()); 
        } </span><span class="s0">else </span><span class="s1">{ 
            howMayTasks.setText(</span><span class="s4">&quot;Ilość produktów na liście zakupów &quot; </span><span class="s1">+ lista.getNameToView() + </span><span class="s4">&quot;: &quot; </span><span class="s1">+ list.getCount()); 
        } 
    } 
 
    </span><span class="s3">/** 
     * Okno dialogowe 
     * </span><span class="s5">@param </span><span class="s3">id nr okna dialogowego 
     * </span><span class="s5">@return </span><span class="s3">okno dialogowe 
     */</span><span class="s1"> 
    @Override 
    </span><span class="s0">protected </span><span class="s1">Dialog onCreateDialog(</span><span class="s0">int </span><span class="s1">id) { 
        </span><span class="s0">switch </span><span class="s1">(id) { 
            </span><span class="s0">case </span><span class="s1">CONFIRM_REMOVE_LIST: { 
                </span><span class="s0">return new </span><span class="s1">AlertDialog.Builder(</span><span class="s0">this</span><span class="s1">) 
                        .setMessage(</span><span class="s4">&quot;Czy chcesz usunąć wybrany produkt?&quot;</span><span class="s1">) 
                        .setPositiveButton(</span><span class="s4">&quot;Tak&quot;</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnClickListener() { 
                            @Override 
                            </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface arg0, </span><span class="s0">int </span><span class="s1">arg1) { 
                                Produkt selectedProdukt = adapter.getSelectedItem(); 
                                adapter.removeItem(adapter.getSelectedItemPos()); 
                                adapter.unselectAll(); 
                                list.setAdapter(adapter); 
                                produktyDao.delete(selectedProdukt); 
                                Toast.makeText(getApplicationContext(), </span><span class="s4">&quot;Usunięto produkt: &quot; </span><span class="s1">+ selectedProdukt.getName(), Toast.LENGTH_LONG).show(); 
                                refreshListView(); 
                                disableBtns(); 
                            } 
                        }).setNegativeButton(</span><span class="s4">&quot;Nie&quot;</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnClickListener() { 
                            @Override 
                            </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface dialogInterface, </span><span class="s0">int </span><span class="s1">i) { 
 
                            } 
                        }).create(); 
            } 
        } 
        </span><span class="s0">return null</span><span class="s1">; 
    } 
 
    </span><span class="s3">/** 
     * Sluchacz klikncięcia na listę 
     */</span><span class="s1"> 
    </span><span class="s0">class </span><span class="s1">SluchaczKliknieciaNaListe </span><span class="s0">implements </span><span class="s1">AdapterView.OnItemClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onItemClick(AdapterView&lt;?&gt; parent, View view, </span><span class="s0">int </span><span class="s1">pos, </span><span class="s0">long </span><span class="s1">id) { 
            adapter.setSelected(pos); 
            enableBtns(); 
        } 
    } 
 
    </span><span class="s3">/** 
     * Sluchacz długiego klikncięcia na listę 
     */</span><span class="s1"> 
    </span><span class="s0">class </span><span class="s1">SluchaczListyDlugieKlikniecie </span><span class="s0">implements </span><span class="s1">AdapterView.OnItemLongClickListener { 
        @Override 
        </span><span class="s0">public boolean </span><span class="s1">onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1, 
                                       </span><span class="s0">int </span><span class="s1">pos, </span><span class="s0">long </span><span class="s1">id) { 
            adapter.setSelected(pos); 
            showDialog(CONFIRM_REMOVE_LIST); 
            </span><span class="s0">return true</span><span class="s1">; 
        } 
    } 
 
    </span><span class="s3">/** 
     * Sluchacz klikncięcia na przycisk dodaj 
     */</span><span class="s1"> 
    </span><span class="s0">private class </span><span class="s1">ButtonAddListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onClick(View view) { 
            Produkt produkt = adapter.getSelectedItem(); 
            Intent intent = </span><span class="s0">new </span><span class="s1">Intent(getApplicationContext(), AddAndEditProductActivity.</span><span class="s0">class</span><span class="s1">); 
            </span><span class="s0">if </span><span class="s1">(produkt != </span><span class="s0">null</span><span class="s1">) 
                intent.putExtra(</span><span class="s4">&quot;produkt&quot;</span><span class="s1">, produkt); 
            intent.putExtra(</span><span class="s4">&quot;lista&quot;</span><span class="s1">, lista); 
            intent.putExtra(</span><span class="s4">&quot;czyEdit&quot;</span><span class="s1">, </span><span class="s4">&quot;nie&quot;</span><span class="s1">); 
            startActivity(intent); 
        } 
    } 
 
    </span><span class="s3">/** 
     * Sluchacz klikncięcia na przycisk edytuj 
     */</span><span class="s1"> 
    </span><span class="s0">private class </span><span class="s1">ButtonEditListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onClick(View view) { 
            Produkt produkt = adapter.getSelectedItem(); 
            Intent intent = </span><span class="s0">new </span><span class="s1">Intent(getApplicationContext(), AddAndEditProductActivity.</span><span class="s0">class</span><span class="s1">); 
            intent.putExtra(</span><span class="s4">&quot;produkt&quot;</span><span class="s1">, produkt); 
            intent.putExtra(</span><span class="s4">&quot;lista&quot;</span><span class="s1">, lista); 
            intent.putExtra(</span><span class="s4">&quot;czyEdit&quot;</span><span class="s1">, </span><span class="s4">&quot;tak&quot;</span><span class="s1">); 
            startActivity(intent); 
        } 
    } 
 
    </span><span class="s3">/** 
     * Sluchacz klikncięcia na przycisk usuń 
     */</span><span class="s1"> 
    </span><span class="s0">private class </span><span class="s1">ButtonDeleteListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onClick(View view) { 
            Produkt selectedProdukt = adapter.getSelectedItem(); 
            adapter.removeItem(adapter.getSelectedItemPos()); 
            adapter.unselectAll(); 
            list.setAdapter(adapter); 
            produktyDao.delete(selectedProdukt); 
            Toast.makeText(getApplicationContext(), </span><span class="s4">&quot;Usunięto produkt: &quot; </span><span class="s1">+ selectedProdukt.getName(), Toast.LENGTH_LONG).show(); 
            refreshListView(); 
            disableBtns(); 
        } 
    } 
 
    @Override 
    </span><span class="s0">public void </span><span class="s1">onBackPressed() { 
        Intent intent = </span><span class="s0">new </span><span class="s1">Intent(getApplicationContext(), MainActivity.</span><span class="s0">class</span><span class="s1">); 
        startActivity(intent); 
    } 
} 
</span></pre>
</body>
</html>